package com.xindaoapp.henghua.fragment;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.xindaoapp.henghua.R;
import com.xindaoapp.henghua.application.XinDaoApplication;
import com.xindaoapp.henghua.entry.PropertyGatherInfo;
import com.xindaoapp.henghua.entry.PropertyGatherInfo.MODIFIEDTYPE;
import com.xindaoapp.henghua.entry.PropertyLabelInfo;
import com.xindaoapp.henghua.entry.ScanData;
import com.xindaoapp.henghua.utils.BitmapUtils;
import com.xindaoapp.henghua.utils.SwipeAdapter;
import com.xindaoapp.henghua.utils.SwipeListView;
import com.xindaoapp.henghua.utils.UpdateDataChanged.OnUpdateDataChangedListener;
import com.xindaoapp.henghua.zbar.CameraTestActivity;

/**
 * 标签
 * @author Administrator
 *
 */
public class LabelFragment extends XDBaseFragment {

	private final String TAG = "LabelFragment";

	private SwipeListView lv_label;
	private ImageView iv_addlabel;
	private TextView tv_scanNums;

	/** 获取设备相关所有信息*/
	private PropertyGatherInfo info = null;

	private String deviceID = "";

	private List<PropertyLabelInfo> labelList = new ArrayList<PropertyLabelInfo>();

	private final OnUpdateDataChangedListener mOnUpdateDataChangedListener = new OnUpdateDataChangedListener() {

		@Override
		public void onUpdateDataChanged() {

			info = XinDaoApplication.get().getUpdateDataChanged().getPropertyGatherInfo(deviceID);

			if (null != info) {
				refresh();
			}

		}
	};

	public static LabelFragment newInstance(Bundle args) {
		final LabelFragment f = new LabelFragment();

		f.setArguments(args);

		return f;
	}

	@Override
	protected void initUi() {
		lv_label = (SwipeListView) findViewById(R.id.side_label_listview);
		iv_addlabel = (ImageView) findViewById(R.id.iv_addlabel);

		tv_scanNums = (TextView) findViewById(R.id.tv_scanNums);

		iv_addlabel.setOnClickListener(mOnClickListener);
	}

	private final OnClickListener mOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.iv_addlabel:
				// zxing扫描
//				Intent intent = new Intent(getActivity(), CaptureActivity.class);
				// zbar扫描
				Intent intent = new Intent(getActivity(), CameraTestActivity.class);
				Bundle mBundle = new Bundle();
				mBundle.putSerializable("info", info);
				intent.putExtras(mBundle);
				startActivity(intent);

				break;

			default:
				break;
			}
		}
	};

	private void initData() {

		final SwipeAdapter adapter = new SwipeAdapter(mContext, labelList);
		adapter.setScanDatas(scanDataList);

		adapter.setOnRightItemClickListener(new SwipeAdapter.onRightItemClickListener() {
			@Override
			public void onRightItemClick(View v, int position) {
				int isMy = labelList.get(position).isModified;
				if (isMy == MODIFIEDTYPE.MODIFIED.ordinal()) {

					XinDaoApplication.get().getDBHelper().deleteLabel(labelList.get(position).tag_code);

					String code = labelList.get(position).tag_code;

					XinDaoApplication.get().getUpdateDataChanged().removePropertyLabelInfo(info.id, code);
					XinDaoApplication.get().getUpdateDataChanged().removeScanData(info.id, code);

					XinDaoApplication.get().getUpdateDataChanged().notifyDataChanged();

					String photpFolder = BitmapUtils.DEFAULT_CACHE_FOLDER + "/" + info.code + "/" + code
							+ "/";

					File file = BitmapUtils.createPath(photpFolder);

					BitmapUtils.delete(file);

				} else {
					Toast.makeText(getActivity(), "该条数据不能删除！", Toast.LENGTH_SHORT).show();
				}

			}
		});

		// 在点击删除后,让删除按钮消失
		adapter.getRightItem(new SwipeAdapter.getRightItem() {
			@Override
			public void getRightItem(View v, int position) {
				if (v.getVisibility() == View.VISIBLE) {
					v.setVisibility(View.INVISIBLE);
				}
			}
		});

		lv_label.setAdapter(adapter);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Log.d(TAG, "onCreate");

		XinDaoApplication.get().getUpdateDataChanged().add(mOnUpdateDataChangedListener);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_label, container, false);

		Log.d(TAG, "onCreateView");

		return mView;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		XinDaoApplication.get().getUpdateDataChanged().remove(mOnUpdateDataChangedListener);
	}

	private List<ScanData> scanDataList;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		Log.d(TAG, "onActivityCreated");

		info = (PropertyGatherInfo) getArguments().getSerializable("info");

		deviceID = info.id;

		Log.d(TAG, "code === " + deviceID);

		refresh();

	}

	private void refresh() {

		if (null != info.propertyLabelInfos) {
			System.out.println("info.propertyWithLabelDataInfo != null");

			labelList = info.propertyLabelInfos;

			if (info.scanDatas != null) {
				scanDataList = info.scanDatas;
			}

			if (labelList.size() != 0) {

				CharSequence text = getString(R.string.detail_label_above_left, labelList.size());
				tv_scanNums.setText(text);
				sort();
				initData();
			} else {
				CharSequence text = getString(R.string.detail_label_above_left, 0);
				tv_scanNums.setText(text);
			}

		} else {
			System.out.println("info.propertyWithLabelDataInfo == null");
			CharSequence text = getString(R.string.detail_label_above_left, 0);
			tv_scanNums.setText(text);
		}

	}

	private void sort() {

		Collections.sort(labelList, new ScanDataComparator());
		List<PropertyLabelInfo> nList = new ArrayList<PropertyLabelInfo>();
		List<PropertyLabelInfo> lList = new ArrayList<PropertyLabelInfo>();
		for (int i = 0; i < labelList.size(); i++) {
			if (labelList.get(i).isModified == MODIFIEDTYPE.MODIFIED.ordinal()) {
				lList.add(labelList.get(i));
			} else {
				nList.add(labelList.get(i));
			}
		}

		labelList.clear();
		labelList.addAll(lList);
		labelList.addAll(nList);

	}

	class ScanDataComparator implements Comparator<PropertyLabelInfo> {
		@Override
		public int compare(PropertyLabelInfo sd2, PropertyLabelInfo sd1) {
			return sd1.create_time.compareTo(sd2.create_time);
		}

	}

}
